// const $ = ele => document.querySelector(ele);
var UserName = false;
var UserPwd = false;
var Code = false;

var username = $('.username').value;

var password = $('.password').value;

//随机验证码
$('.code_text').innerHTML = randCode();
$('.code_text').onclick = function () {
    $('.code_text').innerHTML = randCode();
}

// //用户名验证
$('.username').onmouseout = function () {
    username = $('.username').value;
    var reg = /^[a-zA-Z_$][\w$]{5,11}$/;
    if (reg.test(username)) {
        UserName = true;
        $('.username_span').innerHTML = "√";
        $('.username_span').style.color = "green";
        $('.username_span').style.fontSize = "14px";
    } else {
        UserName = false;
        $('.username_span').innerHTML = "用户名长度3-15位字母或数字且不能以数字开头";
        $('.username_span').style.color = "red";
        $('.username_span').style.fontSize = "14px";
    }

}

//密码
$('.password').onmouseout = function () {
    var reg = /^[\w$]{6,16}$/;
    password = $('.password').value;
    if (reg.test(password)) {
        UserPwd = true;

        $('.password_span').innerHTML = "√";
        $('.password_span').style.color = "green";
        $('.password_span').style.fontSize = "14px";
    } else {
        UserPwd = false;
        $('.password_span').innerHTML = "密码长度应在6-20个字符之间,区分大小写"
        $('.password_span').style.color = "red";
        $('.password_span').style.fontSize = "14px";
    }

}

$('.btn').onclick = async function () {

    const code = $('.code').value;
    if (code) {

        if (code === $('.code_text').innerHTML) {
            Code = true;
        } else {
            Code = false;
            $('.code_span').innerHTML = "验证码错误"
            $('.code_span').style.color = "red";
            $('.code_span').style.fontSize = "14px";
            alert(333)
        }
    } else {

        $('.code_span').innerHTML = "验证码不能为空"
        $('.code_text').innerHTML = randCode();
    }
    console.log(UserName);
    console.log(UserPwd);
    console.log(Code);
    if (UserName && UserPwd && Code) {

        if ($('.xz').checked) {
            alert("注册成功")
            const res = await register({
                username,
                password
            })
            const {
                status,
                msg
            } = res;
            if (status) {
                location.href = 'http://127.0.0.1/mi/%e5%b0%8f%e7%b1%b3/login.html';
            } else {
                alert(msg)
            }
        } else {
            alert("请勾选协议!")
        }

    } else {
        alert("注册失败")
    }
}